﻿using System;
using System.Linq;
using System.Collections.Generic;
using System.Text;
using System.Security.Cryptography;
using Framework.Common.Entry;
using System.Net;
using System.IO;
using System.Configuration;


namespace Recharge.Expand
{
    public static class RechargeHelper
    {

        private const string m_contentType = "application/x-www-form-urlencoded;charset=utf-8";

        #region Public

        public static string Distribute(string source, string mothed,string paramters)
        {
            string content = string.Empty;
            HttpWebRequest request = HttpWebRequest.Create(source) as HttpWebRequest;
            request.Method = mothed;
            request.ContentType = m_contentType;
            using (Stream _stream = request.GetRequestStream())
            {
                _stream.Write(Encoding.UTF8.GetBytes(paramters), 0, Encoding.UTF8.GetByteCount(paramters));
                _stream.Flush();
                using (HttpWebResponse response = (HttpWebResponse)request.GetResponse())
                {
                    if (response.StatusCode == HttpStatusCode.OK)
                    {
                        using (Stream stream = response.GetResponseStream())
                        {
                            using (StreamReader reader = new StreamReader(stream))
                            {
                                content = reader.ReadToEnd();
                            }
                        }
                    }
                }
            }
            return content;
        }

        #endregion Public

        private static readonly string m_key = ConfigurationManager.AppSettings["OPKey"];
        private static readonly string m_format = ConfigurationManager.AppSettings["OPFormat"];
        private static readonly string m_signFormat = ConfigurationManager.AppSettings["OPSignFormat"];
        private static readonly string m_hzw = ConfigurationManager.AppSettings["HZW"];
        private static readonly MD5 m_md5 = new MD5CryptoServiceProvider();
        private const string _input_charset = "utf-8";
        private static readonly DateTime m_time = DateTime.Parse("1970-01-01 00:00:00 GMT");

        public static bool PostTemp(string header,string code, int amount, string order, string server,out string content)
        {
            content = string.Empty;
            bool result = false;
            string sign = MD5Sign(_input_charset, string.Format(m_signFormat, code, amount, order,server, m_key));
            string url = string.Format(m_format, code, amount, order,server, sign);

            HttpWebRequest request = HttpWebRequest.Create(url) as HttpWebRequest;
            request.UserAgent = header;
            using (HttpWebResponse response = (HttpWebResponse)request.GetResponse())
            {
                result = response.StatusCode == HttpStatusCode.OK;
                if (result)
                {
                    using (Stream stream = response.GetResponseStream())
                    {
                        using (StreamReader reader = new StreamReader(stream))
                        {
                            content = reader.ReadToEnd();
                            result = string.Equals(content, "1");
                        }
                    }
                }
            }
            return result;
        }

        public static string PostUser(string header, string code, string resource)
        {
            string content = string.Empty;
            bool result = false;
            string url = string.Format(m_hzw+"?code={0}&resource={1}", code, resource);


            HttpWebRequest request = HttpWebRequest.Create(url) as HttpWebRequest;
            request.UserAgent = header;
            request.ContentType = "application/x-www-form-urlencoded";
            using (HttpWebResponse response = (HttpWebResponse)request.GetResponse())
            {
                result = response.StatusCode == HttpStatusCode.OK;
                if (result)
                {
                    using (Stream stream = response.GetResponseStream())
                    {
                        using (StreamReader reader = new StreamReader(stream))
                        {
                            content = reader.ReadToEnd();
                        }
                    }
                }
            }
            return content;
        }

        #region Private

        private static string MD5Sign(string _input_charset, string prestr)
        {
            StringBuilder sb = new StringBuilder(32);
            byte[] t = m_md5.ComputeHash(Encoding.GetEncoding(_input_charset).GetBytes(prestr));
            for (int i = 0; i < t.Length; i++)
            {
                sb.Append(t[i].ToString("x").PadLeft(2, '0'));
            }

            return sb.ToString();
        }
        #endregion Private

    }
}